//
//  MineViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/8/30.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "MineViewController.h"
#import "MineLoginViewCollection.h"
#import "MineRegisterView.h"

@interface MineViewController ()
@property (nonatomic ,strong)MineLoginViewCollection *mineLoginVC;
@property (nonatomic ,strong)UIView *mineLoginView;
@property (nonatomic, strong)MineRegisterView *mineRegisterVC;
@property (nonatomic, strong)UIView *mineRegisterView;
@end

@implementation MineViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _mineRegisterView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mineRegisterVC = [[MineRegisterView alloc]init];
    _mineRegisterView.backgroundColor = [UIColor whiteColor];
    _mineRegisterVC.view.frame = _mineRegisterView.bounds;
    _mineRegisterView.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
    [_mineRegisterVC.returnBtn addTarget:self action:@selector(returnBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    _mineLoginView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mineLoginVC = [[MineLoginViewCollection alloc]init];
    _mineLoginView.backgroundColor =[UIColor whiteColor];
    _mineLoginVC.view.frame = _mineLoginView.bounds;
    _mineLoginView.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
    [_mineLoginVC.LoginBtn addTarget:self action:@selector(LoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self foundImageView];
    [self foundView];
    [self fountButten];
    [self nightModel];
    self.isSele = YES;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)foundImageView {
    self.imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2)];
    self.imageView.image = [UIImage imageNamed:@"背景.jpg"];
    [self.view addSubview:self.imageView];
    self.imageViewSmall = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 14, SCREEN_HEIGHT / 4 - SCREEN_WIDTH / 14, SCREEN_WIDTH / 7, SCREEN_WIDTH / 7)];
    self.imageViewSmall.image = [UIImage imageNamed:@"头像.jpg"];
    self.imageViewSmall.layer.cornerRadius =   (SCREEN_WIDTH / 7) / 2;
    self.imageViewSmall.layer.masksToBounds = YES;
    [self.view addSubview:self.imageViewSmall];
    self.registerLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2.2 - SCREEN_HEIGHT / 40, SCREEN_HEIGHT / 3 - SCREEN_WIDTH / 13, SCREEN_WIDTH /2,SCREEN_WIDTH / 13)];
    self.registerLable.text = @"登录/注册";
    self.registerLable.textColor = [UIColor whiteColor];
    [self.view addSubview:self.registerLable];
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.imageButton.frame = CGRectMake(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 14, SCREEN_HEIGHT / 4 - SCREEN_WIDTH / 14, SCREEN_WIDTH / 7, SCREEN_WIDTH / 7);
    [self.view addSubview:self.imageButton];
    [ self.imageButton addTarget:self action:@selector(aboutUs:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)foundView {
    self.night = [[UIView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 10 )];
    self.night.backgroundColor = [UIColor colorWithRed:1.000 green:0.727 blue:0.611 alpha:1.000];
    [self.view addSubview:self.night];
    self.collect = [[UIView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10 * 6, SCREEN_WIDTH, SCREEN_HEIGHT / 10 )];
    self.collect.backgroundColor = [UIColor colorWithRed:1.000 green:0.801 blue:0.487 alpha:1.000];
    [self.view addSubview:self.collect];
    self.cache = [[UIView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10 * 7, SCREEN_WIDTH, SCREEN_HEIGHT / 10 )];
    self.cache.backgroundColor = [UIColor colorWithRed:0.812 green:1.000 blue:0.524 alpha:1.000];
    [self.view addSubview:self.cache];
    /** 系统更新 */
    self.update = [[UIView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 5 * 4, SCREEN_WIDTH, SCREEN_HEIGHT / 10 )];
    self.update.backgroundColor = [UIColor colorWithRed:0.637 green:1.000 blue:0.741 alpha:1.000];
    [self.view addSubview:self.update];
    /** 关于我们 */
    self.aboutUs = [[UIView alloc]initWithFrame:CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10*9, SCREEN_WIDTH, SCREEN_HEIGHT / 10 )];
    self.aboutUs.backgroundColor = [UIColor colorWithRed:0.093 green:0.979 blue:0.731 alpha:1.000];
    [self.view addSubview:self.aboutUs];
    
}
- (void)fountButten {
    self.night_Butten = [UIButton buttonWithType:UIButtonTypeCustom];
    self.night_Butten.frame =CGRectMake(0,(SCREEN_HEIGHT  - 44 ) / 10 * 8 , SCREEN_WIDTH, SCREEN_HEIGHT / 10 );
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"day"]) {
        [self.night_Butten setTitle:@"夜间模式" forState:UIControlStateNormal];
    }else{
        [self.night_Butten setTitle:@"白天模式" forState:UIControlStateNormal];
        
    }
    [self.view addSubview:self.night_Butten];
    [self.night_Butten addTarget:self action:@selector(night:) forControlEvents:UIControlEventTouchUpInside];
    self.update_Butten = [UIButton buttonWithType:UIButtonTypeCustom];
    self.update_Butten.frame =CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 10 );
    [self.update_Butten setTitle:@"热门收藏" forState:UIControlStateNormal];
    [self.view addSubview:self.update_Butten];
    [ self.update_Butten addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    self.collect_Butten = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collect_Butten.frame =CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10 * 6, SCREEN_WIDTH, SCREEN_HEIGHT / 10 );
    [self.collect_Butten setTitle:@"指南收藏" forState:UIControlStateNormal];
    [self.view addSubview:self.collect_Butten];
    [ self.collect_Butten addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    self.cache_Butten = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cache_Butten.frame =CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10 * 7, SCREEN_WIDTH, SCREEN_HEIGHT / 10 );
    [self.cache_Butten setTitle:@"清除缓存" forState:UIControlStateNormal];
    [self.view addSubview:self.cache_Butten];
    [ self.cache_Butten addTarget:self action:@selector(cache:) forControlEvents:UIControlEventTouchUpInside];
    self.aboutUp_butten = [UIButton buttonWithType:UIButtonTypeCustom];
    self.aboutUp_butten.frame =CGRectMake(0, (SCREEN_HEIGHT  - 44 ) / 10 * 9, SCREEN_WIDTH, SCREEN_HEIGHT / 10 );
    [self.aboutUp_butten setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:self.aboutUp_butten];
    [ self.aboutUp_butten addTarget:self action:@selector(chickVC:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)switchCon:(UISwitch *)sender {
    if (sender.on == YES) {
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changColor) userInfo:nil repeats:YES];//nstimer 定时器
    }else{
        self.night.backgroundColor = [UIColor blackColor];
        self.collect.backgroundColor =  [UIColor blackColor];
        self.cache.backgroundColor =  [UIColor blackColor];
        self.update.backgroundColor =  [UIColor blackColor];
        self.aboutUs.backgroundColor =  [UIColor blackColor];
        [self.updateTimer invalidate];
        
        
    }
    
}
/**夜间模式*/
- (void)night:(UIButton *)sender {
    if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"day"]) {
        [self.night_Butten setTitle:@"夜间模式" forState:UIControlStateNormal];
        [[JXLDayAndNightManager shareManager] dayMode];
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"day"];
    }else{
        [self.night_Butten setTitle:@"白天模式" forState:UIControlStateNormal];
        [[JXLDayAndNightManager shareManager] nightMode];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"day"];
    }
}
/**收藏*/
- (void)collect:(UIButton *)sender {
    
    self.hidesBottomBarWhenPushed = YES;
    
    CollectViewController *collectVC = [[CollectViewController alloc]init];
    
    [self.navigationController pushViewController:collectVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
    
}
- (void)FoundCacheLable {
    self.labelCache = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 4 * 3, (SCREEN_HEIGHT  - 44 ) / 10 * 7, SCREEN_WIDTH / 4, SCREEN_HEIGHT / 10)];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *str = [NSString stringWithFormat:@"%.2f",[ToolDBS folderSizeAtPath:path]];
    self.labelCache.text = [NSString stringWithFormat:@"%@M",str];
    self.labelCache.textColor =[UIColor whiteColor];
    [self.view addSubview:self.labelCache];
    
    
}
/**清除缓存*/
- (void)cache:(UIButton *)sender {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    [ToolDBS clearCache:path];
    self.labelCache.text = [NSString stringWithFormat:@"%.2f",[ToolDBS folderSizeAtPath:path]];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"小包提醒你" message:@"已经成功清除缓存" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        self.alertController = alertController;
        UIAlertAction *defauleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *defauleAction2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        [self.alertController addAction:defauleAction];
        [self.alertController addAction:defauleAction2];

    
}
/**热门收藏*/
- (void)update:(UIButton *)sender {
    self.hidesBottomBarWhenPushed = YES;
    HotCollectViewController *hotcollectVC = [[HotCollectViewController alloc]init];
    [self.navigationController pushViewController:hotcollectVC animated:YES];
    
    self.hidesBottomBarWhenPushed = NO;
}
/**登录*/

- (void)changColor {
    self.night.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
    
    self.collect.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
    
    self.cache.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
    
    self.update.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
    
    self.aboutUs.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
}

- (void)aboutUs:(UIButton *)sender {
    [UIView animateWithDuration:1 animations:^{
        [_mineLoginView addSubview:_mineLoginVC.view];
        _mineLoginView.transform = CGAffineTransformIdentity;
        [self.view addSubview:_mineLoginView];
    }];
}
- (void)chickVC:(UIButton *)sender {
    [UIView animateWithDuration:1 animations:^{
        [_mineRegisterView addSubview:_mineRegisterVC.view];
        _mineRegisterView.transform = CGAffineTransformIdentity;
        [self.view addSubview:_mineRegisterView];
    }];
}
- (void)LoginBtn:(UIButton *)sender {
    [UIView animateWithDuration:3 animations:^{
        _mineLoginView.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
    }];
}
- (void)returnBtn:(UIButton *)sender {
    [UIView animateWithDuration:2 animations:^{
        _mineRegisterView.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
    }];
}
- (void)nightModel {
    //设置日间和夜间两张状态
    [self.view jxl_setDayMode:^(UIView *view) {
        //设置日间模式
        view.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
    } nightMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarMetricsDefault;
        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
}
@end
